<template>
  <div class="bottomBtn">
    <el-button class="addBtn" v-if="addBtnShow" @click="addShow"
      >+添加发票抬头</el-button
    >
    <div class="addForm" v-else>
      <el-form ref="form" :inline="true" :model="addform">
        <el-form-item>
          <el-input v-model.trim="addform.fullTitle" placeholder="全称">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model.trim="addform.shortTitle" placeholder="简称">
          </el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" class="addSave" @click="addSave" round
        >确认</el-button
      >
      <el-button round @click="cancelSave">取消</el-button>
    </div>
  </div>
</template>

<script>
import {
  addInvoiceTitle,
  editInvoiceTitle,
  invoiceTitlelist
} from "@/BMViews/api/aboutInvoice/aboutInvoice.js";
export default {
  data() {
    return {
      addBtnShow: true, //底部添加按钮显示影藏
      mode: "", //add新增，edit编辑
      addform: {
        fullTitle: "",
        shortTitle: ""
      }
    };
  },
  mounted() {
    this.$bus.$on("titleEvent", (row, addBtnShow, mode) => {
      // data就是参数
      console.log("兄弟传值data:", row, addBtnShow, mode);
      this.addBtnShow = addBtnShow;
      this.addform = JSON.parse(JSON.stringify(row));
      this.mode = mode;
    });
  },
  beforeDestroy() {
    this.$bus.$off("titleEvent");
  },
  methods: {
    // 查询发票抬头的接口
    async getTitleList() {
      let circle_id = {
        circle_id: this.$store.state.user.userInfo.whereCircleId
      };
      //   console.log("circle_id:", circle_id);
      const { rows, code } = await invoiceTitlelist(circle_id);
      if (code == 200) {
        // this.titleList = rows;
        // 新增和编辑后把刷新的父级数据传过去
        this.$bus.$emit("titleList", rows);
      }
    },
    // 新增抬头的接口
    async addTitle() {
      let data = {
        circleId: this.$store.state.user.userInfo.whereCircleId,
        fullTitle: this.addform.fullTitle,
        shortTitle: this.addform.shortTitle
      };
      const res = await addInvoiceTitle(data);
      if (res.code == 200) {
        this.$message.success("新增成功");
        // 清空
        this.addform = {
          fullTitle: "",
          shortTitle: ""
        };
      }
    },
    // 编辑抬头的接口
    async editTitle() {
      const res = await editInvoiceTitle(this.addform);
      if (res.code == 200) {
        this.$message.success("编辑成功");
        // 清空
        this.addform = {
          fullTitle: "",
          shortTitle: ""
        };
        this.addBtnShow = true;
      }
    },
    // 点击了新增抬头
    addShow() {
      this.addBtnShow = false;
      this.mode = "add";
    },
    // 点击了确认
    async addSave() {
      if (this.addform.fullTitle == "") {
        this.$message.warning("发票全称不能为空");
        return;
      }
      // 发请求
      if (this.mode == "add") {
        await this.addTitle();
        // 刷新父级
        await this.getTitleList();
      } else if (this.mode == "edit") {
        await this.editTitle();
        // 刷新父级
        await this.getTitleList();
      }
    },
    cancelSave() {
      this.addBtnShow = true;
      // 清空
      this.addform = {
        fullTitle: "",
        shortTitle: ""
      };
    }
  }
};
</script>

<style lang="scss" scoped>
.bottomBtn {
  position: absolute;
  bottom: 0;
  left: 0;
  /* height: 57px; */
  width: 100%;
  z-index: 9;
  /* border-top: 1px solid #ecedf0; */
  /* background-color: #f8f8f8; */
}
.bottomBtn .addBtn {
  width: 100%;
  border: 0;
  border-top: 1px solid #ecedf0;
  background-color: #f8f8f8;
  line-height: 33px;
  color: #606e84;
}
// 添加表单
.addForm {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  width: 100%;
  border: 0;
  border-top: 1px solid #ecedf0;
  background-color: #f8f8f8;
  padding: 8px 30px;
  line-height: 36px;
  color: #606e84;
  .el-form-item {
    margin-bottom: 0;
    ::v-deep .el-form-item__label {
      float: left;
    }
  }
  .el-select,
  .el-input {
    width: 150px;
  }
  .el-button {
    height: 34px;
    line-height: 34px;
    box-sizing: border-box;
    padding: 0 15px;
  }
  .addSave {
    background-color: #f87a23;
    border-color: #f87a23;
  }
}
</style>
